﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ESF.General.GUI;

#endregion


namespace ESF.WinForms.Dialogs
{
	public class ProgressDialogWPF : Window, IProgressDialog<Window>
	{
		#region Miembros

		private bool _WasCancelled;

		public event Action OnCancel;

		#endregion
		
		
		#region Controles

		private ProgressDialogControlWPF _Control;

		#endregion


		#region Constructores

		public ProgressDialogWPF ()
		{
			Width = 410;
			Height = 160;
			
			WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;

			_Control = new ProgressDialogControlWPF ();
			
			_Control.OnCancel += delegate () 
			{ 
				Hide ();

				if (OnCancel != null)
				{
					OnCancel ();
				}
			};

			_Control.OnOK += delegate () 
			{ 
				Hide ();
			};

			AddChild (_Control);

			HideWhenComplete = true;
			IsCancelable = false;
		}

		#endregion


		#region Methods

		public new void Hide ()
		{
			Dispatcher.Invoke ((Action) delegate { base.Hide (); });
		}


		public void Show (Window pParent)
		{
			Owner = pParent;

			if (!IsVisible)
			{
				base.Show ();
			}
		}


		public void Clear()
		{
			_Control.btnOK.IsEnabled = false;
			_Control.btnCancel.IsEnabled = false;

			_WasCancelled = false;

			WPFHelper.ClearAdvance (_Control.lblCurrentStepName, _Control.prgProgressBar);			
		}

		#endregion


		#region Window Events

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			e.Cancel = true;

			if (_Control.prgProgressBar.Value == 100 || _Control.prgProgressBar.Value == 0)
			{
				Hide ();
			}
		}

		#endregion


		#region Interface Fields

		public bool WasCancelled
		{
			get { return _WasCancelled; }
		}

		public bool HideWhenComplete { set; get; }


		public bool IsCancelable
		{
			set { _Control.btnCancel.IsEnabled = value; }
			get { return _Control.btnCancel.IsEnabled; }
		}

		public double PercentageAdvance100
		{
			set 
			{ 
				WPFHelper.SetValue (_Control.prgProgressBar, value); 

				if (value == 100)
				{
					Dispatcher.Invoke ((Action) delegate () 
					{
						_Control.btnOK.IsEnabled = true;
						_Control.btnOK.Focus ();
						_Control.btnCancel.IsEnabled = false;

						if (HideWhenComplete)
						{
							Hide ();
						}
					});
				}
			}
		}

		public new string Title
		{
			set { WPFHelper.SetTitle (this, value); }
		}

		public string CurrentStepName
		{
			set { WPFHelper.SetText (_Control.lblCurrentStepName, value); }
		}

		#endregion
	}
}
